import requests
from bs4 import BeautifulSoup
from apps.spider import searchEngineSpider


def getNovelHtml(url, header):
    # header = random.choice(header)
    html = requests.get(url, headers=header)
    bs = BeautifulSoup(html.content, "html.parser")
    content = bs.select(".box_con > #list dl dd a")
    urls = []
    p = 0
    for i in content:
        urls.append({"chapter_url": url + "/" +i.attrs["href"], "chapter_title": i.text, "index": p})
        p += 1
    return urls


def parseContent(url="https://www.biquwx.la/69_69509/13051887.html", header=None):
    c = "clickbids=62245; Hm_lvt_8946ff9237ac8dadf2da5e6aef2ec574=1631779827,1631781822,1631785624,1632223472; " \
        "Hm_lvt_6dfe3c8f195b43b8e667a2a2e5936122=1631778103,1632224470; " \
        "Hm_lpvt_6dfe3c8f195b43b8e667a2a2e5936122=1632224470; Hm_lpvt_8946ff9237ac8dadf2da5e6aef2ec574=1632224470 "
    cookie = {i.split("=")[0]: i.split("=")[1] for i in c.split("; ")}
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/93.0.4577.82 Safari/537.36"}

    html = requests.get(url, headers=head, cookies=cookie)
    bs = BeautifulSoup(html.content, "html.parser")
    title = bs.select_one("div.bookname > h1").text
    content = bs.select_one("div#content")
    return {"chapter_title": title, "chapter_content": content}


def test(novelName):
    baidu_links = searchEngineSpider.parseBaidu(novelName)
    novelUrls = []
    if len(baidu_links) != 0:
        novelUrls = getNovelHtml(baidu_links[0].get("baidu_url"), {"User-Agent": searchEngineSpider.randomUA()})

    # a = parseContent(novelUrls[0].get("chapter_url"))
    # print(a)
    return novelUrls



if __name__ == "__main__":
    c = "clickbids=62245; Hm_lvt_8946ff9237ac8dadf2da5e6aef2ec574=1631779827,1631781822,1631785624,1632223472; Hm_lvt_6dfe3c8f195b43b8e667a2a2e5936122=1631778103,1632224470; Hm_lpvt_6dfe3c8f195b43b8e667a2a2e5936122=1632224470; Hm_lpvt_8946ff9237ac8dadf2da5e6aef2ec574=1632224470"
    cookie = {i.split("=")[0]: i.split("=")[1] for i in c.split("; ")}
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"}
    # getNovelHtml("https://www.biquwx.la/62_62245/",{"User-Agent":searchEngineSpider.randomUA()})
    parseContent("https://www.biquwx.la/69_69509/13051887.html", head)
    # test("修界最强赘婿")
